package com.system.cacheCenterBackend.service.cacheService;

import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

/**
 * Caffeine缓存服务实现类
 */
@Service
public class CaffeineCacheServiceImpl implements CaffeineCacheService {

    @Autowired
    private Cache<String, String> caffeineCache;
    
    // 为不同数据类型添加缓存实例
    @Autowired
    private Cache<String, Map<String, String>> hashCache;
    
    @Autowired
    private Cache<String, List<String>> listCache;
    
    @Autowired
    private Cache<String, Set<String>> setCache;
    
    @Autowired
    private Cache<String, Set<String>> zsetCache;

    @Override
    public boolean set(String key, String value) {
        try {
            caffeineCache.put(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean set(String key, String value, long timeout) {
        // Caffeine本身不直接支持为单个键设置不同的过期时间
        // 这里我们仍然将值放入缓存，但忽略timeout参数
        return set(key, value);
    }

    @Override
    public String get(String key) {
        try {
            return caffeineCache.getIfPresent(key);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean delete(String key) {
        try {
            caffeineCache.invalidate(key);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean exists(String key) {
        try {
            return caffeineCache.getIfPresent(key) != null;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取所有缓存项
     *
     * @return 缓存项映射
     */
    public ConcurrentMap<String, String> getAll() {
        return caffeineCache.asMap();
    }
    
    @Override
    public Map<String, String> getHash(String key) {
        try {
            return hashCache.getIfPresent(key);
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public boolean setHash(String key, Map<String, String> value) {
        try {
            hashCache.put(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    @Override
    public List<String> getList(String key) {
        try {
            return listCache.getIfPresent(key);
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public boolean setList(String key, List<String> value) {
        try {
            listCache.put(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    @Override
    public Set<String> getSet(String key) {
        try {
            return setCache.getIfPresent(key);
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public boolean setSet(String key, Set<String> value) {
        try {
            setCache.put(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    @Override
    public Set<String> getZSet(String key) {
        try {
            return zsetCache.getIfPresent(key);
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public boolean setZSet(String key, Set<String> value) {
        try {
            zsetCache.put(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}